package book.spec;

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;

/**
 * 公钥规范，用于还原公钥
 */
public class X509EncodedKeySpecTest {

	public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException {
		
		KeyPairGenerator gen = KeyPairGenerator.getInstance("DSA");
		gen.initialize(512);
		KeyPair key = gen.generateKeyPair();
		
		byte[] publicKeyBytes = key.getPublic().getEncoded();
		System.out.println(new BigInteger(publicKeyBytes));
		
		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
		KeyFactory fac = KeyFactory.getInstance("dsa");
		PublicKey key2 = fac.generatePublic(keySpec);
		System.out.println(new BigInteger(key2.getEncoded()));
	}
}
